/**
 * This file is part of Tamariba. Copyright (C) 2011 Michael Vogt <michael@tamariba.eu>, Patrick Hempel
 * <patrick@tamariba.eu>
 * 
 * Tamariba is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * Tamariba is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Tamariba. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package eu.tamariba.html.user.client;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.CachingActivityMapper;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.inject.Inject;

import eu.tamariba.html.client.scaffold.gae.ReloadOnAuthenticationFailure;
import eu.tamariba.html.user.client.activity.UserActivityMapper;
import eu.tamariba.html.user.client.place.UserDisplayPlace;
import eu.tamariba.html.user.client.place.UserPlaceHistoryFactory;
import eu.tamariba.html.user.client.place.UserPlaceHistoryMapper;
import eu.tamariba.html.user.client.request.UserRequestFactory;
import eu.tamariba.html.user.client.ui.UserDesktopShell;

public class UserDesktopApp extends UserApp {

	private final UserDesktopShell shell;
	private final UserRequestFactory requestFactory;
	private final EventBus eventBus;
	private final PlaceController placeController;
	private final UserPlaceHistoryFactory placeHistoryFactory;
	private final UserActivityMapper statusActivityMapper;

	@Inject
	public UserDesktopApp(UserDesktopShell shell, UserRequestFactory requestFactory, EventBus eventBus,
	                          PlaceController placeController, UserPlaceHistoryFactory placeHistoryFactory,
	                          UserActivityMapper statusActivityMapper) {
		this.shell = shell;
		this.requestFactory = requestFactory;
		this.eventBus = eventBus;
		this.placeController = placeController;
		this.placeHistoryFactory = placeHistoryFactory;
		this.statusActivityMapper = statusActivityMapper;
	}

	public void run() {
		/* Add handlers, setup activities */
		init();

		/* Hide the loading message */
		Element loading = Document.get().getElementById("loading");
		loading.getParentElement().removeChild(loading);

		/* And show the user the shell */
		RootLayoutPanel.get().add(shell);
	}

	private void init() {
        // AppEngine user authentication
        new ReloadOnAuthenticationFailure().register(eventBus);

		CachingActivityMapper activityMapper = new CachingActivityMapper(statusActivityMapper);
		final ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);

		activityManager.setDisplay(shell.getDisplay());
		
		UserPlaceHistoryMapper mapper = GWT.create(UserPlaceHistoryMapper.class);
		mapper.setFactory(placeHistoryFactory);
		PlaceHistoryHandler placeHistoryHandler = new PlaceHistoryHandler(mapper);
		Place defaultPlace = new UserDisplayPlace();
		placeHistoryHandler.register(placeController, eventBus, defaultPlace);
		placeHistoryHandler.handleCurrentHistory();
	}
}